﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using System.Net.Http;
using System.Net.Http.Headers;
using XclusiveClient.Classes;

namespace XclusiveClient
{
    public partial class Registration : System.Web.UI.Page
    {
        //Getting all the Clients
        ArrayList clientList = new ArrayList();
        protected void Page_Load(object sender, EventArgs e)
        {
            HttpClient client = new HttpClient();
            client.BaseAddress = new Uri("http://localhost:3676/");
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

            HttpResponseMessage response = client.GetAsync("api/Client").Result;
            if (response.IsSuccessStatusCode)
            {
                var clients = response.Content.ReadAsAsync<IEnumerable<Client>>().Result;
                foreach (var cs in clients)
                {
                    Client c = new Client();
                    c.username = cs.username;
                    c.full_name = cs.full_name;
                    c.password = cs.password;
                    c.email = cs.email;
                    c.address = cs.address;
                    c.contact_number = cs.contact_number;
                    c.company_country = cs.company_country;
                    c.company_name = cs.company_name;
                    c.company_email = cs.company_email;
                    c.company_address = cs.company_address;
                    c.account_type = cs.account_type;
                    clientList.Add(c);
                }
            }
            else
            {
                lblStatus.Visible = true;
                lblStatus.Text = "Could not register for an account. Error code: " + response.StatusCode + ", Reason: " + response.ReasonPhrase.ToString() + "<br/>";
            }
        }

        protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
        {
            foreach (Client x in clientList)
            {
                if (x.username == tbxusername.Text)
                {
                    args.IsValid = (tbxusername.Text == null);
                }
            }
        }

        protected void tbxusername_TextChanged(object sender, EventArgs e)
        {
            CustomValidator1.Validate();
        }

        protected void registerButton_Click(object sender, EventArgs e)
        {
            Page.Validate();
            if (Page.IsValid)
            {
                //Creating a Client
                Client temp = new Client();
                temp.username = tbxusername.Text;
                temp.full_name = tbxfull_name.Text;
                temp.password = tbxpassword.Text;
                temp.email = tbxemail.Text;
                temp.address = tbxaddress.Text;
                temp.contact_number = Convert.ToInt32(tbxcontact_number.Text);
                temp.company_country = ddcompany_country.SelectedValue;
                temp.company_name = tbxcompany_name.Text;
                temp.company_email = tbxcompany_email.Text;
                temp.company_address = tbxcompany_address.Text;
                temp.account_type = "Administrative";
                //End here for the storing..

                HttpClient client = new HttpClient();
                client.BaseAddress = new Uri("http://localhost:3676/");
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

                var c = new Client()
                {
                    username = temp.username,
                    full_name = temp.full_name,
                    password = temp.password,
                    email = temp.email,
                    address = temp.address,
                    contact_number = temp.contact_number,
                    company_country = temp.company_country,
                    company_name = temp.company_name,
                    company_email = temp.company_email,
                    company_address = temp.company_address,
                    account_type = temp.account_type
                };

                HttpResponseMessage response = client.PostAsJsonAsync("api/Client", c).Result;
                if (response.IsSuccessStatusCode)
                {
                    Session["username"] = temp.username;
                    Session["full_name"] = temp.full_name;
                    Session["email"] = temp.email;
                    Session["address"] = temp.address;
                    Session["contact_number"] = temp.contact_number;
                    Session["company_country"] = temp.company_country;
                    Session["company_name"] = temp.company_name;
                    Session["company_email"] = temp.company_email;
                    Session["company_address"] = temp.company_address;
                    Response.Redirect("ThankYou.aspx");
                }
                else
                {
                    lblStatus.Visible = true;
                    lblStatus.Text = "Could not register for an account. Error code: " + response.StatusCode + ", Reason: " + response.ReasonPhrase.ToString() + "<br/>";
                }
            }
        }

        protected void btnCheck_Click(object sender, EventArgs e)
        {
            string country = Session["company_country"].ToString();

            HttpClient client = new HttpClient();
            String appID = "86f7a6c86cd241dd8577583283c2a392";
            client.BaseAddress = new Uri("http://openexchangerates.org/");
            client.DefaultRequestHeaders.Accept.Add(
                new MediaTypeWithQualityHeaderValue("application/json"));

            HttpResponseMessage ratesResp = client.GetAsync("api/latest.json?appid=" + appID).Result;

            if (ratesResp.IsSuccessStatusCode)
            {
                var result = ratesResp.Content.ReadAsAsync<ConverterResult>().Result;
                double rates = result.rates.SGD; //Try to compare and choose accordingly to selection
                lblrates.Text += "<p>1 USD = " + rates + " " + country + "<p/>"; //display rate
            }
        }
    }
}